
//
// 130327 - AH - Created.
//

package org.japril.swing.jutil.logging2;

import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JToolBar;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.japril.swing.ExceptionDialog;
import org.japril.swing.jutil.logging.ControlLogSinks;
import org.japril.swing.jutil.logging.ControlLogSinks.ControlLogSinkManager;

/**
 * <p>
 *   Tool bar to select and manage 
 *   {@link ControlLogSinkManager}.
 * </p>
 * @author Arne Halvorsen (AH)
 */
@SuppressWarnings("serial")
public final class ControlLogSinksBar extends JToolBar 
{
  private final JComboBox m_confs=new JComboBox();
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public ControlLogSinksBar() 
  {
    add(m_confs);
    add(m_newAction);
    add(m_deleteAction);
    add(m_saveAction);
    updateList();
    
    final ControlLogSinks managers=ControlLogSinks.getInstance();
    
    m_confs.addItemListener
    (
      new ItemListener()
      {
        public void itemStateChanged(ItemEvent e)
        {
          Object s=m_confs.getSelectedItem();
          if (s instanceof ControlLogSinkManager)
          {  
            if (managers!=null)
            {
              managers.setCurrent((ControlLogSinkManager)s);
            }
          }
        }
      }
    );
    
    if (managers!=null)
    {
      managers.addChangeListener
      (
        new ChangeListener()
        {
          public void stateChanged(ChangeEvent e)
          {
            if (managers!=null)
            {
              ControlLogSinkManager clsm=managers.getCurrent();
              if (clsm!=null)
              {
                System.out.println("in bar set current: "+clsm.getName());
                m_confs.setSelectedItem(clsm);
              }
            }
          }
        }
      );  
    }
    
  }
  
  private void updateList()
  {
    m_confs.removeAllItems();
    ControlLogSinks managers=ControlLogSinks.getInstance();
    if (managers!=null)
    {
      for (ControlLogSinkManager curr : managers) m_confs.addItem(curr);
    }
    
    ControlLogSinkManager curr=managers.getCurrent();
    if (curr!=null) m_confs.setSelectedItem(curr);
  }
  
  private final Action m_newAction=new AbstractAction("New")
  {
    public void actionPerformed(ActionEvent e)
    {
      ControlLogSinks managers=ControlLogSinks.getInstance();
      if (managers!=null)
      {
        String name=JOptionPane.showInputDialog("Configuration's Name?");
        name=(name==null) ? "" : name.trim();
        if (name.length()>0)
        {
          if (managers.exists(name))
          {
            ExceptionDialog.showErrorDialog("Named configuration exists");
          }
          else
          {
            try
            {
              ControlLogSinkManager manager=managers.create(name);
              managers.setCurrent(manager);
              updateList();
            }
            catch (IOException iox)
            {
              ExceptionDialog.showErrorDialog(iox);
            }
          }
        }
      }
    }
  };
  
  private final Action m_deleteAction=new AbstractAction("Delete")
  {
    public void actionPerformed(ActionEvent e)
    {
      ControlLogSinkManager clsm=
        (ControlLogSinkManager)m_confs.getSelectedItem();
      if (clsm!=null)
      {
        ControlLogSinks managers=ControlLogSinks.getInstance();
        if (managers!=null)
        {
          if (managers.delete(clsm))
          {
            m_confs.removeItem(clsm);
          }
        }
      }
    }

  };
  
  private final Action m_saveAction=new AbstractAction("Save")
  {  
    public void actionPerformed(ActionEvent e)
    {
      try
      {
        ControlLogSinks managers=ControlLogSinks.getInstance();
        if (managers!=null) managers.save();
      }
      catch (IOException iox)
      {
        iox.printStackTrace();
      }
    }
  }; 
  
}
